<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">

<head>
    <title>搜索插件</title>
    <link href="autocomplete.css" rel="stylesheet" type="text/css" />
    <style type="text/css">
    #divtest {
        width: 282px;
    }

    #divtest .title {
        padding: 8px;
        background-color: Blue;
        color: #fff;
        height: 23px;
        line-height: 23px;
        font-size: 15px;
        font-weight: bold;
    }

    #divtest .content {
        padding: 8px 0px;
        background-color: #fff;
        font-size: 13px;
    }

    #divtest .content .tip {
        text-align: center;
        border: solid 1px #ccc;
        background-color: #eee;
        margin: 20px 0px;
        padding: 8px;
        display: none;
    }

    .fl {
        float: left;
    }

    .fr {
        float: right;
    }
    </style>
    <script type="text/javascript" src="http://www.imooc.com/data/jquery-1.8.2.min.js"></script>
    <script src="autocomplete.js" type="text/javascript"></script>
</head>

<body>
    <div id="divtest">
        <div class="title">
            <span class="fl">搜索插件</span>
        </div>
        <div class="content">
            <span class="fl">用户名</span>
            <br />
            <input id="txtSearch" name="txtSearch" type="text" />
            <div class="tip">
            </div>
        </div>
    </div>
    <script type="text/javascript">
    $(function() {
        var arrUserName = ["菜鸟一号", "菜鸟二号", "菜鸟三号", "菜鸟四号", "菜鸟五号", "菜鸟六号", "菜鸟七号", "菜鸟八号", "菜鸟都是菜鸟", ];
        $('#txtSearch').autocomplete(arrUserName, {
            minChars: 0, //双击空白文本框时显示全部提示数据
            formatItem: function(data, i, total) {
                return "<I>" + data[0] + "</I>"; //改变匹配数据显示的格式
            },
            formatMatch: function(data, i, total) {
                return data[0];
            },
            formatResult: function(data) {
                return data[0];
            }
        }).result(SearchCallback);

        function SearchCallback(event, data, formatted) {
            $(".tip").show().html("您的选择是：" + (!data ? "空" : formatted));
        }
    });
    </script>
</body>

</html>